package com.japstudy.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.japstudy.entity.User;
import com.japstudy.service.IUserService;

@Controller
@RequestMapping("/Admin")
public class AdminController {
	
	@Autowired
	private IUserService userServiceImpl;
	
	@RequestMapping("/qdAPIUI")
	public String qdAPIUI() {
		return "admin/qdAPI";
	}
	
	@RequestMapping("/loginUi")
	public String loginUi() {
		return "admin/login";
	}
	
	@RequestMapping("/indexUi")
	public String indexUi() {
		return "admin/index";
	}
	
	@RequestMapping("/login")
	public String login(User user, HttpServletRequest request, HttpServletResponse response) throws IOException {
		User loginAdmin = userServiceImpl.adminLogin(user);
		if(loginAdmin != null) {
			request.getSession().setAttribute("loginAdmin", loginAdmin);
			response.sendRedirect(request.getContextPath() + "/Admin/indexUi");
			return null;
		} else {
			response.sendRedirect(request.getContextPath() + "/Admin/loginUi");
			return null;
		}
	}
	
	@RequestMapping("/loginOut")
	public String loginOut(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Object loginAdmin = request.getSession().getAttribute("loginAdmin");
		if(loginAdmin != null) {
			request.getSession().removeAttribute("loginAdmin");
		}
		response.sendRedirect(request.getContextPath() + "/Admin/loginUi");
		return null;
	}
}
